/**
 * ...
 * @author Krzysztof Rozalski
 */

package hapi.util;
import hapi.log.LogLevel;
import haxe.Stack;

class Try {
	
	static inline var NO_PARAMS = [];
	
	public static function toRun(instance:Dynamic, method:Void->Dynamic, ?args:Array<Dynamic> = null) {
		if (args == null) args = NO_PARAMS;
		try {
			return Reflect.callMethod(instance, method, args);
		}catch (e:Dynamic) {
			trace(e, LogLevel.error);
			var stackString = Stack.toString(Stack.exceptionStack());
			trace(stackString);
			throw e;
		}
	}
	
	public static function toCallback(instance:Dynamic, method:Dynamic, ?args:Array<Dynamic> = null) {
		if (args == null) args = NO_PARAMS;
		return callback(toRun, instance, method, args);
	}
}